package pingu.interface_graphique;

import java.awt.Font;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class MainMenuState extends BasicGameState{
	 
    Image background = null;
    Image startGame = null;
    Image multiGame = null;
    Image joinGame = null;
 
    int stateID = 0;
 
 
    private static int menuX = 150;
    private static int menuGame = 160;
    private static int menuMulti = 320;
    private static int menuJoin = 480;

    float startGameScale = 1;
    float multiGameScale = 1;
    float joinGameScale = 1;
   TrueTypeFont trueTypeFont = null;
	private float scaleButtonGame=1;
	private float scaleButtonMulti=1;
	private float scaleButtonJoin=1;
 
 
    public MainMenuState(int stateID )
    {
        this.stateID = stateID;
    }
 
    @Override
    public int getID() {
        return stateID;
    }
 
 
 
    public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
        background = new Image("img/menu.png");
 
        // Load the menu images
        startGame = new Image("img/start.png");
        multiGame = new Image("img/multiHeberger.png");
        joinGame = new Image("img/multiRejoindre.png");

 
        //--------------------------------------------------
 
        Font font = new Font("Verdana", Font.BOLD, 20);
        trueTypeFont = new TrueTypeFont(font, true);
        
        float largeurRatio=(float)gc.getWidth()/(float)background.getWidth();
        float hauteurRatio=(float)gc.getHeight()/(float)background.getHeight();
        startGameScale=Math.max(largeurRatio, hauteurRatio);
        multiGameScale=Math.max(largeurRatio, hauteurRatio);
        joinGameScale=Math.max(largeurRatio, hauteurRatio);

    }
 
 
 
    public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException {
        // render the background
        background.draw(0, 0,startGameScale);
 
        // Draw menu
        startGame.draw(menuX, menuGame, startGameScale*scaleButtonGame);
        multiGame.draw(menuX, menuMulti, startGameScale*scaleButtonMulti);
        joinGame.draw(menuX, menuJoin, startGameScale*scaleButtonJoin);
 
 
    }
 
    float scaleStep = 0.0001f;
 
    public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
        Input input = gc.getInput();
 
        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();

        boolean insideStartGame = false;
        boolean insideMultiGame = false;
        boolean insideJoinGame = false;
 
        if( ( mouseX >= menuX && mouseX <= startGameScale*scaleButtonGame*startGame.getWidth() +menuX  ) &&
            ( mouseY >= menuGame && mouseY <= startGame.getHeight()*startGameScale*scaleButtonGame+menuGame ) )
        {
            insideStartGame = true;
        } 
        if( ( mouseX >= menuX && mouseX <= multiGameScale*scaleButtonMulti*multiGame.getWidth() +menuX  ) &&
                ( mouseY >= menuMulti && mouseY <= multiGame.getHeight()*multiGameScale*scaleButtonMulti+menuMulti ) )
            {
                insideMultiGame = true;
            }
        if( ( mouseX >= menuX && mouseX <= joinGameScale*scaleButtonJoin*joinGame.getWidth() +menuX  ) &&
                ( mouseY >= menuJoin && mouseY <= joinGame.getHeight()*joinGameScale*scaleButtonJoin+menuJoin ) )
            {
                insideJoinGame = true;
            }        
        if(insideStartGame)
        {
            if(scaleButtonGame < 1.05f)
            	scaleButtonGame += scaleStep * delta;
            if(scaleButtonMulti > 1.0f)
            	scaleButtonMulti -= scaleStep * delta;
            if(scaleButtonJoin > 1.0f)
            	scaleButtonJoin -= scaleStep * delta;
 
            if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                sb.enterState(PinguGame.GAMEPLAYSTATE);
            }
        } else if(insideMultiGame){
        	if(scaleButtonMulti < 1.05f)
            	scaleButtonMulti += scaleStep * delta;
            if(scaleButtonGame > 1.0f)
            	scaleButtonGame -= scaleStep * delta;
            if(scaleButtonJoin > 1.0f)
            	scaleButtonJoin -= scaleStep * delta;
 
            if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                sb.enterState(PinguGame.MULTIHOSTSTATE);
            }
        } else if(insideJoinGame){
        	if(scaleButtonJoin < 1.05f)
            	scaleButtonJoin += scaleStep * delta;
            if(scaleButtonGame > 1.0f)
            	scaleButtonGame -= scaleStep * delta;
            if(scaleButtonMulti > 1.0f)
            	scaleButtonMulti -= scaleStep * delta;            
 
            if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ){
                sb.enterState(PinguGame.MULTIJOINSTATE);
            }
        }
        else{
            if(scaleButtonGame > 1.0f)
            	scaleButtonGame -= scaleStep * delta;
            if(scaleButtonMulti > 1.0f)
            	scaleButtonMulti -= scaleStep * delta;
 
           // if ( input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) )
           //     gc.exit();
        }
 
    }
 
}